//
//  MainController.swift
//  swift-微博
//
//  Created by chenrin on 2016/12/13.
//  Copyright © 2016年 zhuoheng. All rights reserved.
//

import UIKit

class MainController: UITabBarController {
    // MARK:- 懒加载
    fileprivate lazy var composeBtn : UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .highlighted)
        btn.addTarget(self, action: #selector(composeBtnClick(button:)), for: .touchUpInside)
        return btn
    }()
   
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置自控制器
        addAllChilds()
       }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //添加加号按钮
        
        //调整加号按钮的位置
        let width = UIScreen.main.bounds.size.width / 5
        let height = 49
        let rect = CGRect(x: 0, y: 0, width: Int(width), height: height)
        composeBtn.bounds = rect
        composeBtn.center = CGPoint(x: UIScreen.main.bounds.size.width / 2, y:CGFloat(height / 2))
        tabBar.addSubview(composeBtn)
        
    }
}
// MARK:- 初始化自控制器
extension UITabBarController{
    
    
    fileprivate func addAllChilds(){
        //设置字体和图片的选中颜色是orange
        tabBar.tintColor = UIColor.orange
        //1.获取json文件路径
        let path = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil)
        //2.通过文件路径创建NSData
        if let jsonPath = path{
            let jsonData = NSData(contentsOfFile: jsonPath)
            //3.序列化json数据 -> Array
            do{
                let dictArr = try JSONSerialization.jsonObject(with: jsonData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String:String]]
                //4.遍历数组，动态创建控制器和设置数据
                for (index,dict) in dictArr.enumerated(){
                    if index == 2 {
                        addChildViewControllers(childControllerName: "NullViewController", title: "", imageName: "")
                        continue
                    }
                    addChildViewControllers(childControllerName: dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
                }
            }catch{
                //发生异常的时候本地创建
                //2.创建首页
                addChildViewControllers(childControllerName: "HomeController", title: "首页", imageName: "tabbar_home")
                
                addChildViewControllers(childControllerName: "MessageController", title: "消息", imageName: "tabbar_message_center")
                addChildViewControllers(childControllerName: "NullViewController", title: "", imageName: "")
                addChildViewControllers(childControllerName: "MessageController", title: "广场", imageName: "tabbar_discover")
                
                addChildViewControllers(childControllerName: "ProfileController", title: "我的", imageName: "tabbar_profile")
            }
        }
    }
    
    private func addChildViewControllers(childControllerName:String,title:String,imageName:String){
        
        //-1.动态获取命名空间
       let ns = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        //0.将字符串转换为类
        let clsString = ns + "." + childControllerName
        let cls:AnyClass? = NSClassFromString(clsString)
        
        //0.1 通过类创建一个对象
        let vcCls = cls as! UIViewController.Type
        
        let vc = vcCls.init()
        
        
        //1.1设置首页tabbar对应的数据
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_highlighted")
        vc.title = title
        //2.给首页包装一个导航控制器
        let nav = UINavigationController()
        nav.addChildViewController(vc)
        //3.将导航控制器添加到当前控制器上
        addChildViewController(nav)
    }
}

// MARK:- 回调方法
extension UITabBarController{
    func composeBtnClick(button:UIButton) {
        
    }
}

